//
//  CircleProView.m
//  OriginalDemo
//
//  Created by 马东朝 on 2018/5/10.
//  Copyright © 2018年 曹杰. All rights reserved.
//

#import "CircleProView.h"
#define kLineWid kWidth(12);
@interface CircleProView ()
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (strong, nonatomic)CAShapeLayer *ovalSShapeLayer;
@property (strong, nonatomic)CAShapeLayer *redShapeLayer;
@end
@implementation CircleProView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        mainFrame = frame;
    }
    return self;
}
- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self creatLayer];
}
- (void)drawRect:(CGRect)rect{
//    [self creatLayer];
}
-(void)creatLayer{
    
    //添加底层白色圆环
    self.ovalSShapeLayer = [[CAShapeLayer alloc]init];
    //圆环颜色
    self.ovalSShapeLayer.strokeColor = [UIColor clearColor].CGColor;
    //内部填充颜色
    self.ovalSShapeLayer.fillColor = [UIColor clearColor].CGColor;
    //线宽
    self.ovalSShapeLayer.lineWidth = kLineWid;
    //半径
    CGFloat ovalRadius = mainFrame.size.height/2-kLineWid;
    self.ovalSShapeLayer.path =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(mainFrame.size.width/2 - ovalRadius, mainFrame.size.height/2 - ovalRadius, ovalRadius * 2, ovalRadius * 2)].CGPath;
    self.ovalSShapeLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:self.ovalSShapeLayer];
    
    //添加填充圆环
    self.redShapeLayer = [[CAShapeLayer alloc]init];
    self.redShapeLayer.strokeColor = [UIColor greenColor].CGColor;
    self.redShapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.redShapeLayer.lineWidth = kLineWid;
    
    self.redShapeLayer.path =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(mainFrame.size.width/2 - ovalRadius, mainFrame.size.height/2 - ovalRadius, ovalRadius * 2, ovalRadius * 2)].CGPath;
    self.redShapeLayer.strokeStart = 0;
    self.redShapeLayer.strokeEnd = _progress;
    
    [self.layer addSublayer:self.redShapeLayer];
    //strokeStart为0时是从3点钟方向开始，故将其旋转270度从12点钟方向开始
    self.transform = CGAffineTransformMakeRotation((M_PI * 2) / 4 * 3);
    [self gradientLayer];
    [self addAnimation];
    
}

-(void)addAnimation{
    
    CABasicAnimation * strokeEndAnimate = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    strokeEndAnimate.fromValue = [NSNumber numberWithFloat:0.0];
    strokeEndAnimate.toValue = [NSNumber numberWithFloat:_progress];
    
    CAAnimationGroup *strokeAnimateGroup = [CAAnimationGroup animation];
    strokeAnimateGroup.duration = 1.5;
    strokeAnimateGroup.repeatCount = 1;
    strokeAnimateGroup.animations = @[strokeEndAnimate];
    
    [self.redShapeLayer addAnimation:strokeAnimateGroup forKey:nil];
    
    
}
- (CAGradientLayer *)gradientLayer {
    if (!_gradientLayer) {
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.frame = self.bounds;
        _gradientLayer.colors = @[(id)[UIColor cyanColor].CGColor,
                                  (id)[UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000].CGColor];
        [_gradientLayer setStartPoint:CGPointMake(0.5, 1.0)];
        [_gradientLayer setEndPoint:CGPointMake(0.5, 0.0)];
        
        [_gradientLayer setMask:self.redShapeLayer];
        [self.layer addSublayer:_gradientLayer];
        
    }
    return _gradientLayer;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
